Use case: User application program in polling mode

Use case applicable in HAL2 for both FLASH and NVM.

Note

In HAL2, FLASH programming APIs have been streamlined with clearer and more flexible parameters. The legacy TypeProgram, FlashAddress, and DataAddress pattern is replaced by buffer-based APIs.

HAL2 provides specialized programming APIs like HAL_FLASH_{area}_ProgramByAddr for specific FLASH regions such as OTP, EDATA, and OBK.

  • HAL_FLASH_ProgramByAddr to program a user flash area

  • HAL_FLASH_OTP_ProgramByAddr to program an OTP flash area

  • HAL_FLASH_EDATA_ProgramByAddr to program an EDATA flash area

  • HAL_FLASH_OBK_ProgramByAddr to program an OB Key flash area

Sequence diagram

User application program in polling mode (HAL1)

@startuml
title: User application program in polling mode (HAL1)

hide footbox
participant "User Application" as User
participant "FLASH" as FLASH

group #snow FLASH Unlock
    User -> FLASH : HAL_FLASH_Unlock()
end

group #snow FLASH Program Operation (loop)
    loop while Address < FLASH_USER_END_ADDR
        User -> FLASH : HAL_FLASH_Program(FLASH_TYPEPROGRAM_GRANULARITY, Address, GRANULARITY)
        alt Program success (HAL_OK)
            note right: Increment Address by 32
            User --> User : Address = Address + 32
        else Program failure
            User --> User : Handle error or retry
        end
    end
end

group FLASH Lock (recommended)
    User -> FLASH : HAL_FLASH_Lock()
end

@enduml

Important

In HAL2, the programming APIs handle the entire data buffer in a single call, eliminating the need for explicit looping over FLASH units.

Code snippet

HAL1

HAL2

uint32_t Address = FLASH_USER_START_ADDR;
const uint32_t QUADWORD[4] = {
  0x12345678, 0x87654321, 0x12344321, 0x56788765
};

/* Unlock the FLASH to enable the FLASH control register access */
HAL_FLASH_Unlock();

while (Address < FLASH_USER_END_ADDR)
{
  /* Program FLASH memory */
  if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD,
                       Address,
                       (uint32_t)QUADWORD) == HAL_OK)
  {
    Address = Address + 32U; /* increment for the next FLASH word */
  }
}

/* Lock the FLASH to disable the FLASH control register access */
HAL_FLASH_Lock();
hal_flash_handle_t flash_handle;
uint32_t address = FLASH_START_ADDR;

/* FLASH initialization */
HAL_FLASH_Init(&flash_handle, HAL_FLASH);

/* Unlock the FLASH to enable the FLASH control register access */
HAL_FLASH_ITF_Unlock(HAL_FLASH);

#if defined (USE_HAL_FLASH_PROGRAM_BY_ADDR) && (USE_HAL_FLASH_PROGRAM_BY_ADDR == 1U)

/* Set the programming mode according to the FLASH granularity */
HAL_FLASH_SetProgrammingMode(&flash_handle, HAL_FLASH_PROGRAM_QUADWORD);

/* Program FLASH memory using the granularity size in bytes */
/* area = User flash, EDATA, OTP */
HAL_FLASH_ProgramByAddr(&flash_handle, address, data_buf, QUADWORD_SIZE_BYTES, TIMEOUT);

#endif /* USE_HAL_FLASH_PROGRAM_BY_ADDR */

/* Lock the FLASH to disable the FLASH control register access */
HAL_FLASH_ITF_Lock(HAL_FLASH);

/* FLASH deinitialization */
HAL_FLASH_DeInit(&flash_handle);

Key changes

  1. Buffer-based programming replaces the HAL1 per-granule loop (single call programs a full buffer).

  2. Programming APIs are specialized per area in HAL2 (USER/OTP/EDATA/OBK) via HAL_FLASH_{area}_ProgramByAddr.

  3. HAL2 uses an explicit handle + IO/ITF split (init/deinit in IO, unlock/lock in ITF).